package com.a85.wxhelper;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


import com.a85.wxhelper.util.Constants;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

import static com.a85.wxhelper.util.Constants.PACKAGE_NAME;
import static de.robv.android.xposed.XposedHelpers.callMethod;
import static de.robv.android.xposed.XposedHelpers.callStaticMethod;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;

/**
 * @author hejiangwei
 */
public class CommonHook {
    private static final String TAG = CommonHook.class.getSimpleName();

    private static volatile CommonHook commonHook;

    private Handler handler;
    public static String wechatVersionName="";
    public Object thisObject=null;



    public  void setThisObject(Object thisObject) {
        if (this.thisObject==null){
            this.thisObject = thisObject;
        }
    }

    public Object getThisObject() {
        return thisObject;
    }

    public String getCursor(String username) {
        String result=null;
        String select = String.format("select * from rcontact where username = '%s'", username);
        Cursor cursor= (Cursor) XposedHelpers.callMethod(thisObject, Constants.TAG_CURSOR, select, new String[]{}, 2);
        if (cursor!=null) {
            if (cursor.getCount()>0) {
                cursor.moveToFirst();
                result=cursor.getString(cursor.getColumnIndex("nickname"));
            }
            cursor.close();
        }
        return result;
    }




    public String getCursor() {
        String result=null;
        String select = ("select * from addr_upload2");
        Cursor cursor= (Cursor) XposedHelpers.callMethod(thisObject, Constants.TAG_CURSOR, select, new String[]{}, 2);
        if (cursor!=null) {

                Log.e(TAG, "getCursor===="+cursor.getCount());
            if (cursor.getCount()>0) {
                while (cursor.moveToNext()){
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                Log.e(TAG, "getCursor===="+cursor.getString(i) );

                    }
                }
            }
            cursor.close();
        }
        return result;
    }





    public CommonHook(){
        getHandler();
    }
    public static CommonHook getInstance() {
        if (commonHook == null) {
            synchronized (CommonHook.class) {
                if (commonHook == null) {
                    commonHook = new CommonHook();
                }
            }
        }
        return commonHook;
    }

    public void showToast(final Context context, final String content){
        getHandler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,content,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getHandler() {
        if (handler==null){
            handler=new Handler(Looper.getMainLooper());
        }
    }

    public Context getContext() {
        final Class<?> aClass = findClass("android.app.ActivityThread", null);
        Object[] object = new Object[0];
        final Object currentActivityThread = callStaticMethod(aClass, "currentActivityThread", object);
        return  (Context) callMethod(currentActivityThread, "getSystemContext", object);
    }


    public  void initWechatVersion(Context context) {
        if (context==null) {
            return;
        }
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
            if (pInfo != null) {
                wechatVersionName = pInfo.versionName;
        showToast(context, "wechat-version=" + CommonHook.wechatVersionName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void hook_method(Class<?> clazz, String methodName, Object... parameterTypesAndCallback) {
        try {
            findAndHookMethod(clazz, methodName, parameterTypesAndCallback);
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }

    public static void hook_method(String className, ClassLoader classLoader, String methodName,
                                   Object... parameterTypesAndCallback) {
        try {
            findAndHookMethod(className, classLoader, methodName, parameterTypesAndCallback);
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }

    public static void hook_methods(String className, String methodName, XC_MethodHook xmh) {
        try {
            Class<?> clazz = Class.forName(className);

            for (Method method : clazz.getDeclaredMethods())
                if (method.getName().equals(methodName)
                        && !Modifier.isAbstract(method.getModifiers())
                        && Modifier.isPublic(method.getModifiers())) {
                    XposedBridge.hookMethod(method, xmh);
                }
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }

}
